package org.zjx.analysis.flag;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.SneakyThrows;
import org.zjx.core.IWindow;
import org.zjx.util.ImageUtil;
import org.zjx.analysis.BaseAnalysis;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseFlagAnalysis extends BaseAnalysis {
    public static int DEVIATION = 10;  //场景对应的实际address坐标 红点误差10坐标
    protected List<FlagPoint> points;

    public BaseFlagAnalysis(IWindow window, BufferedImage screenshot) {
        super(window, screenshot);
    }

    @Override
    protected void analysis() {

        if (points == null) {
            points = new ArrayList<>();
        }

        //获取实际内容图片
        Rectangle rectangle = new Rectangle(getContentArea().x - 8, getContentArea().y, getContentArea().width + 16, getContentArea().height + 8);
        BufferedImage contentImage = getSpecifiedAreaImage(rectangle);
        List<Point> redPoints = ImageUtil.matchTemplate(contentImage, getRedImage());
        for (Point point : redPoints) {
            point = new Point(point.x - 8 + 6, point.y + 6);
            points.add(location(point));
        }
    }

    //获取合成旗内容区域
    protected abstract Rectangle getContentArea();

    //获取实际对应地图大小
    protected abstract Point getMapSize();

    protected BufferedImage getRedImage() {
        String image = "iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAABJklEQVR42n2QoZaDMBBFI/oBSERFxIqICkQFogKxAlGBqEAiKipWVKxAVCARFRVIPqASUbFiP6ASsQKxojKSP5jODEnI2T2n4h04ybvvzUTozyOMTQXj7QrjtxH9t3hWow4FPPIc+m0C1ySG8pSBGM8zMPzcWQ7EO/1xmKFNBJdIImRaGPjt/0HctMscVK/UDNkmfffGq0rQ+wL6LOXRLghUSprxWgPePMCOhi1faexaShWC4IeghRuj8/QA+jjtYgFqKd8k5GGAENZTIsEsNNOZ32CBQgaQLxEa8JIMlErfwSzdvXsjeUAsBAhKs2JjMqfXuDTt4AMMdWjojImN1mzTcYdoIRzAED2hFRldcjil/wUYoksyOeMLs4PSteSfVyZfKgjgCc1ic+93OM8NAAAAAElFTkSuQmCC";
        return ImageUtil.stringToBufferedImage(image);
    }

    //获取合成旗上面所有的坐标点位
    public List<Point> getLocations() {
        List<Point> points = new ArrayList<>();
        for (FlagPoint point : this.points) {
            points.add(point.getLocation());
        }
        return points;
    }

    //获取指定坐标对应的红点屏幕坐标
    public Point getLocation(Point point) {
        for (FlagPoint flagPoint : points) {
            Point location = flagPoint.getLocation();
            if (Math.abs(point.x - location.x) < DEVIATION && Math.abs(point.y - location.y) < DEVIATION) {
                Point mapPoint = flagPoint.getPoint();
                return new Point(windowArea.x + getContentArea().x + mapPoint.x, windowArea.y + getContentArea().y + mapPoint.y);
            }
        }
        return null;
    }

    //计算红点对应坐标
    @SneakyThrows
    protected FlagPoint location(Point point) {
        double proportionX = (double) getMapSize().x / (getContentArea().getWidth());
        double proportionY = (double) getMapSize().y / (getContentArea().getHeight());
        // 坐标系转换
        int translatedY = (int) getContentArea().getHeight() - point.y;
        // 比例计算 + 向上取整
        return new FlagPoint(point, new Point((int) Math.ceil((point.x + 1) * proportionX), (int) Math.floor(proportionY * translatedY)));
    }

    @Getter
    @AllArgsConstructor
    public static class FlagPoint {
        private Point point;
        private Point Location;
    }
}
